﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;


namespace BD2MKV
{
    public partial class FormMovie : Form
    {
        public string label = null;
        public string movieId = null;
        public string language = null;
        public string movieName = null;

        
        private Dictionary<string, string> movies = new Dictionary<string,string>();

        public FormMovie()
        {
            InitializeComponent();
        }

        private void listViewShows_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewShows.SelectedItems.Count > 0)
            {
                textBoxDescription.Text = movies[listViewShows.SelectedItems[0].Text];
                buttonOk.Enabled = true;
                
                movieId = listViewShows.SelectedItems[0].Text;
                movieName = listViewShows.SelectedItems[0].SubItems[1].Text;
                language = listViewShows.SelectedItems[0].SubItems[2].Tag.ToString();
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            listViewShows.Items.Clear();


            XmlTextReader reader = new XmlTextReader("http://api.themoviedb.org/2.1/Movie.search/en/xml/"+ TheMovieDB.tmdbApiKey + "/" + textBoxMovie.Text);
            Console.WriteLine(label);
            string tmpMovieId = "";
            string tmpLanguage = "eng";
            string tmpMovieName = "";
            string tmpOverview = "";
            string tmpReleased = "";

            try
            {
                reader.Read();
            }
            catch (Exception ex)
            {
                textBoxDescription.Text = ex.Message.ToString();
                return;
            }

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);
                        Console.WriteLine(">");

                        switch (reader.Name)
                        {
                            case "id":
                                reader.Read(); // Movie Id
                                tmpMovieId = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "name":
                                reader.Read(); // Movie Name
                                tmpMovieName = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "overview":
                                reader.Read(); // Overview
                                tmpOverview = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            case "released":
                                reader.Read(); // Released
                                tmpReleased = reader.Value;
                                Console.WriteLine(reader.Value);
                                break;
                            default:
                                break;
                        }
                        
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        Console.WriteLine(reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        switch (reader.Name)
                        {
                            case "movie":
                                movies[tmpMovieId] = tmpOverview;

                                ListViewItem listViewItem1 = new ListViewItem();
                                listViewItem1.Text = tmpMovieId;
                                listViewItem1.SubItems.Add(tmpMovieName);
                                listViewItem1.SubItems.Add(LanguageCodes.GetName(tmpLanguage));
                                listViewItem1.SubItems[2].Tag = tmpLanguage;
                                listViewItem1.SubItems.Add(tmpReleased);
                                listViewShows.Items.Add(listViewItem1);

                                listViewShows.Refresh();

                                tmpMovieId = "";
                                tmpLanguage = "";
                                tmpMovieName = "";
                                tmpOverview = "";
                                tmpReleased = "";

                                break;
                            default:
                                break;
                        }
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                    default:
                        Console.WriteLine(reader.Value);
                        break;
                }
            }           
        }

        private void FormMovie_Load(object sender, EventArgs e)
        {
            textBoxMovie.Text = label;
            buttonSearch_Click(this, EventArgs.Empty);
            
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {

        }


        
    }
}
